package com.chb.service.impl;

import com.chb.dao.BranchDao;
import com.chb.dao.StaffDao;
import com.chb.dao.StaffTransferDao;
import com.chb.dao.TransferDao;
import com.chb.model.entity.Branch;
import com.chb.model.entity.Shopkeeperchange;
import com.chb.model.entity.Staff;
import com.chb.model.entity.StaffTransfer;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.model.vo.StaffTransferVO;
import com.chb.service.StaffExService;
import com.chb.service.StaffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author ：张家琪
 * @date ：Created in 2023/10/10 15:37
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class StaffExServiceImpl implements StaffExService {
    @Autowired
    private StaffTransferDao staffTransferDao;
    @Autowired
    private BranchDao branchDao;
    @Autowired
    private HttpSession session;
    @Autowired
    private StaffDao staffDao;
    @Autowired
    private TransferDao transferDao;

    /**
     * @param
     * @param current
     * @param pageSize
     * @param branchid
     * @return com.chb.model.vo.Result
     * @create by: 张77
     * @description: 获取员工审核的所有数据
     * @create time: 2023/10/10 15:37
     */
    @Override
    public Result getAllInfo(Integer current, Integer pageSize, Integer branchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        //1.设置页数
        PageHelper.startPage(current, pageSize);
        List<StaffTransferVO> allStaffTransferInfo = null;
        //2.进行查询
        if (branchid != null) {
            //模糊查询
            allStaffTransferInfo = staffTransferDao.getAllStaffTransferInfo(staff.getStaffid(), branchid, 1);
        } else {
            //初始查询
            allStaffTransferInfo = staffTransferDao.getAllStaffTransferInfo(staff.getStaffid(), staff.getBranchid(), 2);
        }
        //3.设置返回数据
        PageInfo<StaffTransferVO> pageInfo = new PageInfo<>(allStaffTransferInfo);
        //4.返回分页vo
        PageBean<StaffTransferVO> pageBean = new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    //查询所有的店名
    @Override
    public Result getallBranchName() {
        Staff staff = (Staff) session.getAttribute("staff");
        List<Branch> allBranch = branchDao.findAllBranch(staff.getBranchid(), staff.getStaffid());
        return new Result(200, "查询成功", allBranch);
    }


    /**
     * @param staffExService
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 进行入库审核
     * @create time: 2023/10/12 15:42
     */
    @Override
    public Result updateStatus(StaffTransfer staffExService) {
        Staff staff = (Staff) session.getAttribute("staff");
        //修改状态
        staffExService.setExtime(new Date(System.currentTimeMillis()));
        staffExService.setExid(staff.getStaffid());

        Integer index = staffTransferDao.updateState(staffExService);
        if (index > 0) {
            if (staffExService.getState().equals(2)) {
                StaffTransfer staffTransferById = staffTransferDao.getStaffTransferById(staffExService.getId());
                /*修改审核状态*/
                Staff staff1 = new Staff();
                staff1.setBranchid(staffTransferById.getNewbranchid());
                staff1.setDeptid(staffTransferById.getNewdeptid());
                staff1.setStaffid(staffTransferById.getStaffid());
                staffDao.updateBranchid(staff1);

                /*添加调动信息*/
                //获取角色信息
                Staff staffByStaffId = staffDao.findStaffByStaffId(staffTransferById.getStaffid());

                Shopkeeperchange shopkeeperchange = new Shopkeeperchange();
                shopkeeperchange.setStaffid(staffTransferById.getStaffid());
                shopkeeperchange.setBranchid(staffTransferById.getBranchid());
                shopkeeperchange.setDeptid(staffTransferById.getDeptid());
                shopkeeperchange.setRoleid(staffByStaffId.getRoleid());

                shopkeeperchange.setNewbranchid(staffTransferById.getNewbranchid());
                shopkeeperchange.setNewdeptid(staffTransferById.getNewdeptid());
                shopkeeperchange.setNewroleid(staffByStaffId.getRoleid());
                shopkeeperchange.setTransfertime(new Date(System.currentTimeMillis()));
                shopkeeperchange.setTransferper(staff.getStaffid());
                transferDao.addTransferDao(shopkeeperchange);
            }
        }
        return null;
    }
}